package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.work.wb.dao.WbContractDao;
import com.cyt.work.wb.model.po.WbContract;
import com.cyt.work.wb.model.po.WxWbBulid;
import com.cyt.work.wb.model.vo.WbContractVo;
import com.cyt.work.wb.model.vo.WxWbBulidVo;
import com.cyt.work.wb.service.WbContractService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 维保模块-合同表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:26:18
 */
@Service("wbContractService")
public class WbContractServiceImpl extends BaseServiceImpl<WbContract> implements WbContractService {

    @Resource
    private WbContractDao wbContractDao;

    @Autowired
    public void setBaseDao(WbContractDao wbContractDao) {
        this.baseDao = wbContractDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbContractVo 对象
     * @return List 对象集合
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:18
     */
    @Override
    public List<WbContract> listExample(WbContractVo wbContractVo) {
        Example example = new Example(WbContract.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(wbContractVo.getName())) {
            criteria.andLike("name","%"+wbContractVo.getName()+"%");
        }
        if (StringUtils.isNotBlank(wbContractVo.getNum())) {
            criteria.andLike("num","%"+wbContractVo.getNum()+"%");
        }
        if (StringUtils.isNotBlank(wbContractVo.getApplyId())) {
            criteria.andEqualTo("applyId",wbContractVo.getApplyId());
        }
        return wbContractDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbContractVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:18
     */
    @Override
    public ReturnResult page(WbContractVo wbContractVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbContract> list = listExample(wbContractVo);
        PageInfo<WbContract> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbContractVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbContractVo wbContractVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbContractVo.getId())) {
            WbContract wbContract = new WbContract(sysUser);
            wbContract.setApplyId(wbContractVo.getApplyId());
            wbContract.setName(wbContractVo.getName());
            wbContract.setNum(wbContractVo.getNum());
            wbContract.setSignDate(DateUtil.parseDate(wbContractVo.getSignDate()));
            wbContract.setEndDate(DateUtil.parseDate(wbContractVo.getEndDate()));
            wbContract.setSupplier(wbContractVo.getSupplier());
            wbContract.setPrice(wbContractVo.getPrice());
            wbContract.setContent(wbContractVo.getContent());
            wbContractDao.insert(wbContract);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbContract wbContract = wbContractDao.selectByPrimaryKey(wbContractVo.getId());
            wbContract.setApplyId(wbContractVo.getApplyId());
            wbContract.setName(wbContractVo.getName());
            wbContract.setNum(wbContractVo.getNum());
            wbContract.setSignDate(DateUtil.parseDate(wbContractVo.getSignDate()));
            wbContract.setEndDate(DateUtil.parseDate(wbContractVo.getEndDate()));
            wbContract.setSupplier(wbContractVo.getSupplier());
            wbContract.setPrice(wbContractVo.getPrice());
            wbContract.setContent(wbContractVo.getContent());
            wbContract.setUpdateTime(new Date());
            wbContract.setUpdateUser(sysUser.getId());
            wbContractDao.updateByPrimaryKey(wbContract);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbContractVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbContractVo wbContractVo, SysUser sysUser) {
        //要修改的字段
        WbContract wbContract = new WbContract();
        wbContract.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbContract.setUpdateUser(sysUser.getId());
        wbContract.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbContract.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbContractVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbContractVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbContractDao.updateByExampleSelective(wbContract, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbContractVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:26:18
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbContractVo wbContractVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbContract.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbContractVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbContractVo.getId().split(",")));
        }
        wbContractDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 获取合同树（）
     *
     * @param wbContractVo 参数接受对象
     * @return ReturnResult
     * @author dingxu
     * @date 2019-8-15 17:15
     */
    @Override
    public ReturnResult dTreeList(WbContractVo wbContractVo) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<WbContract> wbContractList = this.listExample(wbContractVo);
        for (WbContract wbContract : wbContractList) {
            Map<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("id", wbContract.getId());
            resultMap.put("title", wbContract.getName());
            resultMap.put("parentId", "0");//没有父级 全部置为0
            Map<String, String> map = new LinkedHashMap<>();
            map.put("type", "0");
            map.put("isChecked", "0");
            List<Map<String, String>> checkArrList = new ArrayList<>();
            checkArrList.add(map);
            resultMap.put("checkArr", checkArrList);
            resultList.add(resultMap);
        }
        return new ReturnResult(resultList);
    }
}